package com.alibaba.dubbo.demo.provider.dao.impl;

import java.util.List;

import org.apache.log4j.Logger;

import com.alibaba.dubbo.demo.SysBackuppolicyInterface;
import com.alibaba.dubbo.demo.bean.PageBean;
import com.alibaba.dubbo.demo.bean.SysBackuplog;
import com.alibaba.dubbo.demo.bean.SysBackuppolicy;

public class SysBackuppolicyDaoImpl extends BaseDAO  implements SysBackuppolicyInterface {

	Logger logger = Logger.getLogger(SysBackuppolicyDaoImpl.class);
	@Override
	public boolean saveBackupPolicy(SysBackuppolicy policy) {
		// TODO Auto-generated method stub
		try {
			getHibernateTemplate().save(policy);
			return true;
		} catch (Exception e) {
			logger.error("新增备份策略！", e);
			System.out.println("新增备份策略异常:"+e.getMessage());
			return false;
		}
	}

	@Override
	public boolean updateBackupPolicy(SysBackuppolicy policy) {
		// TODO Auto-generated method stub
		try {
			getHibernateTemplate().update(policy);
			return true;
		} catch (Exception e) {
			logger.error("更新备份策略！", e);
			System.out.println("更新备份策略异常:"+e.getMessage());
			return false;
		}
	}

	@Override
	public boolean deleteBackupPolicy(SysBackuppolicy policy) {
		// TODO Auto-generated method stub
		try {
			getHibernateTemplate().delete(policy);
			return true;
		} catch (Exception e) {
			logger.error("删除备份策略！", e);
			System.out.println("删除备份策略异常:"+e.getMessage());
			return false;
		}
	}

	@Override
	public boolean deleteUnvalidBackupPolicy() {
		// TODO Auto-generated method stub
		String msql = "delete from sys_backuppolicy where backuptype=3 and lasttime <> '' " ;
		try
		{
			excuteBySql(msql);
			return true;
		}
		catch (Exception e) {
			logger.error("执行"+msql+"出错", e);
			return false;
		}
	}

	@Override
	public PageBean queryAllBackupPolicy(PageBean pageBean, boolean valid) {
		// TODO Auto-generated method stub
		String msql = " from SysBackuppolicy ";
		if(valid)             
			msql +=" where (backuptype = 3 and lasttime <> '') or (backuptype <> 3) ";
		
		String countSql = "select count(*) " + msql;
		return getPageBean(msql, countSql, pageBean);
	}

	@Override
	public List<SysBackuppolicy> queryAllBackupPolicyList(boolean valid) {
		// TODO Auto-generated method stub
		String msql = " from SysBackuppolicy ";
		if(valid)
			msql +=" where (backuptype = 3 and lasttime <> '') or (backuptype <> 3) ";
		return queryBean(msql);		
	}

	@Override
	public SysBackuppolicy queryOneBackupPolicy(int id) {
		// TODO Auto-generated method stub
		try {
			String msql = "from SysBackuppolicy where mainid="+id;
			List<SysBackuppolicy> SysBackuppolicyList = queryBean(msql);
			if(SysBackuppolicyList.size()>0)
				return SysBackuppolicyList.get(0);
			else
				return null;
		} catch (Exception e) {
			return null;
		}
	}
}
